package com.wbdgj.ui.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.PayModel;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.model.ResultPayModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.InformaticaActivity;
import com.wbdgj.ui.mine.VerifiedActivity;
import com.wbdgj.utils.AuthResult;
import com.wbdgj.utils.OrderInfoUtil2_0;
import com.wbdgj.utils.PayResult;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


import java.text.DecimalFormat;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/6/21.
 */

public class RechargeActivity extends BaseActivity {

    private Context context = this;

    @BindView(R.id.syBtn)
    Button syBtn;
    @BindView(R.id.esBtn)
    Button esBtn;
    @BindView(R.id.wsBtn)
    Button wsBtn;
    @BindView(R.id.ybBtn)
    Button ybBtn;
    @BindView(R.id.ebBtn)
    Button ebBtn;
    @BindView(R.id.wbBtn)
    Button wbBtn;

    @BindView(R.id.zfbBtn)
    Button zfbBtn;
    @BindView(R.id.wxBtn)
    Button wxBtn;

    @BindView(R.id.qtEdit)
    EditText qtEdit;

    @BindView(R.id.money)
    TextView money;

    @BindView(R.id.zh)
    TextView zh;
    @BindView(R.id.ye)
    TextView ye;

    private Boolean isV = true;
    private Boolean isZfb = true;

    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;

    private String choiceMoney = "10";
    private String ORDER_CODE ;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        Toast.makeText(context, "支付成功", Toast.LENGTH_SHORT).show();
                        getMbUserInfo();
                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        Toast.makeText(context, "支付失败", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        Toast.makeText(context,
                                "授权成功\n" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT)
                                .show();
                    } else {
                        // 其他状态值则为授权失败
                        Toast.makeText(context,
                                "授权失败" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT).show();

                    }
                    break;
                }
                default:
                    break;
            }
        };
    };


    @Override
    protected int getLayoutResId() {
        return R.layout.ui_recharge;
    }

    @Override
    protected void initView() {
        qtEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                choiceMoney = editable.toString();
                money.setText(editable.toString());
            }
        });
    }

    private void rechargepre() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().rechargepre(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), ""+choiceMoney, "1");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    ORDER_CODE = linkedTreeMap.get("ORDER_CODE")+"";
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("alipays://platformapi/startapp?saId=10000007&qrcode="+linkedTreeMap.get("qr_code")+""));
                    startActivityForResult(intent, 9);
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void rechargepreWx() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().rechargepre(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), ""+choiceMoney, "2");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    ORDER_CODE = linkedTreeMap.get("ORDER_CODE")+"";
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(linkedTreeMap.get("qr_code")+""));
                    startActivityForResult(intent, 9);
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void apppay() {
        Call<ResultPayModel<PayModel>> call = HttpAdapter.getSerives().apppay(choiceMoney, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultPayModel<PayModel>>(this) {
            @Override
            protected void onSuccess(ResultPayModel<PayModel> userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    String app_id = userModelBaseResultModel.getData().getApp_id();
                    String biz_content = userModelBaseResultModel.getData().getBiz_content();
                    String charset = userModelBaseResultModel.getData().getCharset();
                    String format = userModelBaseResultModel.getData().getFormat();
                    String method = userModelBaseResultModel.getData().getMethod();
                    String notify_url = userModelBaseResultModel.getData().getNotify_url();
                    String sign1 = userModelBaseResultModel.getData().getSign();
                    String sign_type = userModelBaseResultModel.getData().getSign_type();
                    String timestamp = userModelBaseResultModel.getData().getTimestamp();
                    String version = userModelBaseResultModel.getData().getVersion();

                    Map<String, String> params = OrderInfoUtil2_0.buildOrderParamMap(app_id, biz_content, charset, method, sign_type, timestamp, version, sign1, notify_url, format);
                    String orderParam = OrderInfoUtil2_0.buildOrderParam(params);
                    Log.e("zp", "orderParam="+orderParam);

                    final String orderInfo = orderParam ;
                    Runnable payRunnable = new Runnable() {
                        @Override
                        public void run() {
                            PayTask alipay = new PayTask(RechargeActivity.this);
                            Map<String, String> result = alipay.payV2(orderInfo, true);
                            Log.i("msp", result.toString());

                            Message msg = new Message();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };

                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void apppaywx() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().apppaywx(choiceMoney, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    IWXAPI api = WXAPIFactory.createWXAPI(context, null);//通过工厂创建对象
                    api.registerApp(linkedTreeMap.get("appid")+"");
                    PayReq request = new PayReq();
                    request.appId = linkedTreeMap.get("appid")+"";
                    request.nonceStr = linkedTreeMap.get("noncestr")+"";
                    request.packageValue = linkedTreeMap.get("package")+"";
                    request.partnerId = linkedTreeMap.get("partnerid")+"";
                    request.prepayId = linkedTreeMap.get("prepayid")+"";
                    request.timeStamp = decimalFormat.format(linkedTreeMap.get("timestamp"))+"";
                    request.sign = linkedTreeMap.get("sign")+"";
//                                request.extData = "app data"; // optional
                    // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
                    api.sendReq(request);

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }



    @OnClick({R.id.zhLay, R.id.syBtn, R.id.esBtn, R.id.wsBtn, R.id.ybBtn, R.id.ebBtn, R.id.wbBtn, R.id.zfbBtn, R.id.wxBtn, R.id.qtEdit, R.id.ljczBtn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.zhLay:
                if (isV){
                    launch(InformaticaActivity.class);
                }else{
                    launch(VerifiedActivity.class);
                }
                break;
            case R.id.syBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_on);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");

                choiceMoney = "10";
                money.setText("10");
                break;
            case R.id.esBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_on);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");
                choiceMoney = "20";
                money.setText("20");
                break;
            case R.id.wsBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_on);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");
                choiceMoney = "50";
                money.setText("50");
                break;
            case R.id.ybBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_on);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");
                choiceMoney = "100";
                money.setText("100");
                break;
            case R.id.ebBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_on);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");
                choiceMoney = "200";
                money.setText("200");
                break;
            case R.id.wbBtn:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_on);
                qtEdit.setBackgroundResource(R.mipmap.xz_off);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                qtEdit.setTextColor(getResources().getColor(R.color.text_black));

                qtEdit.setFocusable(false);
                qtEdit.setText("其他");
                choiceMoney = "500";
                money.setText("500");
                break;
            case R.id.qtEdit:
                syBtn.setBackgroundResource(R.mipmap.xz_off);
                esBtn.setBackgroundResource(R.mipmap.xz_off);
                wsBtn.setBackgroundResource(R.mipmap.xz_off);
                ybBtn.setBackgroundResource(R.mipmap.xz_off);
                ebBtn.setBackgroundResource(R.mipmap.xz_off);
                wbBtn.setBackgroundResource(R.mipmap.xz_off);
                qtEdit.setBackgroundResource(R.mipmap.xz_on);

                syBtn.setTextColor(getResources().getColor(R.color.text_black));
                esBtn.setTextColor(getResources().getColor(R.color.text_black));
                wsBtn.setTextColor(getResources().getColor(R.color.text_black));
                ybBtn.setTextColor(getResources().getColor(R.color.text_black));
                ebBtn.setTextColor(getResources().getColor(R.color.text_black));
                wbBtn.setTextColor(getResources().getColor(R.color.text_black));
                qtEdit.setTextColor(getResources().getColor(R.color.colorPrimary));

                qtEdit.setFocusable(true);
                qtEdit.setText("");
                qtEdit.setHint("输入金额");
                qtEdit.setFocusableInTouchMode(true);
                qtEdit.requestFocus();
                break;
            case R.id.zfbBtn:
                zfbBtn.setBackgroundResource(R.mipmap.xz_on);
                wxBtn.setBackgroundResource(R.mipmap.xz_off);
                zfbBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                wxBtn.setTextColor(getResources().getColor(R.color.text_black));
                isZfb = true;
                break;
            case R.id.wxBtn:
                isZfb = false;
                zfbBtn.setBackgroundResource(R.mipmap.xz_off);
                wxBtn.setBackgroundResource(R.mipmap.xz_on);
                zfbBtn.setTextColor(getResources().getColor(R.color.text_black));
                wxBtn.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case R.id.ljczBtn:
                if (isZfb){
//                    apppay();
                    rechargepre();
                }else{
//                    apppaywx();
                    rechargepreWx();
//                    IWXAPI api = WXAPIFactory.createWXAPI(context, null);//通过工厂创建对象
//                    api.registerApp("wx111550af3a0efdd7");
//                    PayReq req = new PayReq();
//                    req.appId		= "wx111550af3a0efdd7";
//                    req.partnerId	= "1515021431";
//                    req.prepayId	= "wx30111242337850299422ba2f4042126396";
//                    req.nonceStr	= "RRfwtnoq7O2HBHn5OLIPArzsrPmdha9V";
//                    req.timeStamp	= "1540869162";
//                    req.packageValue = "Sign=WXPay";
//                    req.sign		= "9264CAFDE03C82D303993ECFC66D189A";
////                    req.extData		= "app data"; // optional
//                    Toast.makeText(context, "正常调起支付", Toast.LENGTH_SHORT).show();
//                    // 在支付之前，如果应用没有注册到微信，应该先调用IWXMsg.registerApp将应用注册到微信
//                    api.sendReq(req);
                }
                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMbUserInfo();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 9){
            Log.e("zp", "支付宝" );
            query();
        }
    }

    private void query() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().query(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), ""+ORDER_CODE);
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("status")+"").equals("1")){
                        launch(RechargeResultSuccessActivity.class);
                    }else if ((linkedTreeMap.get("status")+"").equals("0")){
                        startActivity(new Intent(context, RechargeResultFailActivity.class).putExtra("result", "待支付"));
//                        ToastUtils.toastShort("未支付!");
                    }else if ((linkedTreeMap.get("status")+"").equals("-1")){
                        startActivity(new Intent(context, RechargeResultFailActivity.class).putExtra("result", "支付失败"));
                    }else{
                        startActivity(new Intent(context, RechargeResultFailActivity.class).putExtra("result", "支付失败"));
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    Intent intent = new Intent(context, LoginActivity.class);
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("NAME")+"").equals("null")){
                        launch(VerifiedActivity.class);
                        finish();
                        zh.setText("您还未进行实名认证，请进行实名认证！");
                        isV = false;
                    }else if((linkedTreeMap.get("NAME")+"").equals("")){
                        launch(VerifiedActivity.class);
                        finish();
                        zh.setText("您还未进行实名认证，请进行实名认证！");
                        isV = false;
                    }else{
                        isV = true;
                        zh.setText(linkedTreeMap.get("NAME")+"");
                    }

                    if ((linkedTreeMap.get("CARD_BALANCE")+"").equals("null")){
                        ye.setText("￥0");
                    }else{
                        ye.setText("￥"+decimalFormat.format(linkedTreeMap.get("CARD_BALANCE")));
                    }
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

}
